package cn.iocoder.yudao.module.crossborder.logistics.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.crossborder.logistics.api.dto.LogisticsDTO;
import cn.iocoder.yudao.module.crossborder.logistics.api.dto.TrackingDTO;
import cn.iocoder.yudao.module.crossborder.logistics.service.LogisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 物流管理
 *
 * @author LHL
 */
@Tag(name = "管理后台 - 物流管理")
@RestController
@RequestMapping("/crossborder/logistics")
@Validated
public class LogisticsController {

    @Resource
    private LogisticsService logisticsService;

    @GetMapping("/get")
    @Operation(summary = "获得物流记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:logistics:query')")
    public CommonResult<LogisticsDTO> getLogistics(@RequestParam("id") Long id) {
        LogisticsDTO logistics = logisticsService.getLogistics(id);
        return success(logistics);
    }

    @GetMapping("/list")
    @Operation(summary = "获得物流记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crossborder:logistics:query')")
    public CommonResult<List<LogisticsDTO>> getLogisticsList(@RequestParam("ids") List<Long> ids) {
        List<LogisticsDTO> logisticsList = logisticsService.getLogisticsList(ids);
        return success(logisticsList);
    }

    @PostMapping("/create")
    @Operation(summary = "创建物流记录")
    @PreAuthorize("@ss.hasPermission('crossborder:logistics:create')")
    public CommonResult<Long> createLogistics(@RequestBody @Valid LogisticsDTO logistics) {
        Long id = logisticsService.createLogistics(logistics);
        return success(id);
    }

    @GetMapping("/tracking/get")
    @Operation(summary = "获取物流跟踪信息")
    @Parameter(name = "trackingNumber", description = "跟踪号", required = true, example = "SF1234567890")
    @PreAuthorize("@ss.hasPermission('crossborder:logistics:query')")
    public CommonResult<TrackingDTO> getTracking(@RequestParam("trackingNumber") String trackingNumber) {
        TrackingDTO tracking = logisticsService.getTracking(trackingNumber);
        return success(tracking);
    }

    @PostMapping("/freight/calculate")
    @Operation(summary = "计算运费")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "1024")
    @Parameter(name = "weight", description = "重量(kg)", required = true, example = "1.5")
    @Parameter(name = "destination", description = "目的地", required = true, example = "上海")
    @PreAuthorize("@ss.hasPermission('crossborder:logistics:calculate')")
    public CommonResult<BigDecimal> calculateFreight(@RequestParam("orderId") Long orderId,
                                                    @RequestParam("weight") BigDecimal weight,
                                                    @RequestParam("destination") String destination) {
        BigDecimal freight = logisticsService.calculateFreight(orderId, weight, destination);
        return success(freight);
    }

    @PostMapping("/ship")
    @Operation(summary = "发货")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "1024")
    @Parameter(name = "providerId", description = "物流服务商编号", required = true, example = "2048")
    @PreAuthorize("@ss.hasPermission('crossborder:logistics:ship')")
    public CommonResult<Boolean> ship(@RequestParam("orderId") Long orderId,
                                     @RequestParam("providerId") Long providerId) {
        Boolean result = logisticsService.ship(orderId, providerId);
        return success(result);
    }

    @PostMapping("/update-status")
    @Operation(summary = "更新物流状态")
    @Parameter(name = "logisticsId", description = "物流编号", required = true, example = "1024")
    @Parameter(name = "status", description = "状态", required = true, example = "2")
    @PreAuthorize("@ss.hasPermission('crossborder:logistics:update')")
    public CommonResult<Boolean> updateStatus(@RequestParam("logisticsId") Long logisticsId,
                                             @RequestParam("status") Integer status) {
        Boolean result = logisticsService.updateStatus(logisticsId, status);
        return success(result);
    }

}
